﻿using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using FrameWork.UI.Controls;

namespace FrameWork.UI.ControlsEx
{
    /// <summary>
    /// NbDialogBox.xaml 的交互逻辑
    /// </summary>
    public partial class NbDialogBox : NbWindow
    {
        public static NbDialogBoxResult Notity(string message)
        {
            return ShowDialog(message, 0, "提示", NbMessageBoxButtons.Ok, NbMessageBoxButtons.Ok);
        }
        public static NbDialogBoxResult ShowDialog(string message, int waitTime = 0, string OkText = "确定", string CancelText = "取消")
        {
            return ShowDialog(message, waitTime, "提示", NbMessageBoxButtons.Ok | NbMessageBoxButtons.Cancel, NbMessageBoxButtons.Ok, OkText, CancelText);
        }
        public static NbDialogBoxResult ShowDialog(string message, int waitTime, string title, NbMessageBoxButtons buttons, NbMessageBoxButtons focusButton, string OkText = "确定", string CancelText = "取消")
        {
            var box = new NbDialogBox();
            var result = NbDialogBoxResult.Cancel;
            Application.Current.Dispatcher.Invoke(() =>
            {
                box.MessageLabel.Text = message;
                box.TitleLabel.Content = title;
                box.OkButton.Content = OkText;
                box.CancelButton.Content = CancelText;
                box.WindowStartupLocation = WindowStartupLocation.CenterScreen;

                box.CancelButton.Visibility = buttons.HasFlag(NbMessageBoxButtons.Cancel) ? Visibility.Visible : Visibility.Collapsed;
                box.OkButton.Visibility = buttons.HasFlag(NbMessageBoxButtons.Ok) ? Visibility.Visible : Visibility.Collapsed;
                if (focusButton == NbMessageBoxButtons.Ok)
                    box.OkButton.Focus();
                else if (focusButton == NbMessageBoxButtons.Cancel)
                    box.CancelButton.Focus();

                if (waitTime > 0)
                {
                    var m_Timer = new DispatcherTimer();
                    m_Timer.Interval = new System.TimeSpan(10000000);
                    int i = 0;
                    m_Timer.Tick += new System.EventHandler((v, m) =>
                    {
                        box.OkButton.Content = OkText + "(" + (waitTime - i).ToString() + ")";
                        box.OkButton.IsEnabled = false;
                        box.CancelButton.IsEnabled = false;
                        if (i == waitTime)
                        {
                            result = NbDialogBoxResult.Ok;
                            box.Close();
                        }
                        i++;
                    });
                    m_Timer.Start();
                }

                box.ShowDialog();
                result = box.NbDialogResult;
            });
            return result;
        }


        public NbDialogBox()
        {
            InitializeComponent();
            this.KeyDown += Esc_Exit_KeyDown;
        }

        //监听键盘按键事件，如果按ESC则关闭该窗口
        private void Esc_Exit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        public NbDialogBoxResult NbDialogResult { get; set; } = NbDialogBoxResult.Cancel;

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.NbDialogResult = NbDialogBoxResult.Cancel;
            this.Close();
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            this.NbDialogResult = NbDialogBoxResult.Ok;
            this.Close();
        }
    }

    public enum NbDialogBoxResult
    {
        Unknown = 0,
        Ok = 1,
        Yes = 2,
        No = 3,
        Cancel = 4,
    }

    public enum NbDialogBoxButtons
    {
        Ok = 0,
        YesNo = 1,
        YesNoCancel = 2,
        OkCancel = 3,
        Cancel = 4,
    }
}